package com.wc.算法提高课.A第一章_动态规划.状态机模型.股票买卖V;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/10 19:24
 * @description https://www.acwing.com/problem/content/1060/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, INF = 0x3f3f3f3f;
    // f[i][j] 表示第i只股票,
    //状态为j的最大收益,
    //j = 0,手上有货,
    //j = 1表示手上第一天没货,
    //j = 2表示手上第二天或者是多余第二天没货
    static int[][] f = new int[N][3];
    static int[] w = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) w[i] = sc.nextInt();
        for (int i = 0; i <= n; i++) Arrays.fill(f[i], -INF);
        f[0][2] = 0;
        for (int i = 1; i <= n; i++) {
            f[i][0] = Math.max(f[i - 1][0], f[i - 1][2] - w[i]);
            f[i][1] = f[i - 1][0] + w[i];
            f[i][2] = Math.max(f[i - 1][1], f[i - 1][2]);
        }
        int res = 0;
        for (int i = 0; i <= n; i++) res = Math.max(Math.max(f[i][1], f[i][2]), res);
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
